﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZhengShouXuKeShenQingControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack) {
                Bind();
            }
            Button btnsearch = (Button)FindControl("btnsearch");
            btnsearch.Click += new EventHandler(Go_Search);
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            GridList.OnLoadData += new EventHandler(GridList_OnLoadData);
        }

        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }

        protected void Bind()
        {
            
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            GridList.RowDataBound += new GridViewRowEventHandler(GridList_RowDataBound);
            List<EluZSProvider.Project> project = _EluZSDataProvider.GetProject(GetSqlWhere());
            GridList.DataSource = project;
            GridList.RecordCount = project.Count;
            GridList.PageSize = 10;
            GridList.DataKeyNames = new string[] { "ID", "补偿实施提交状态", "补偿实施审批状态" };
            GridList.DataBind();
            
            
        }
        protected void GridList_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void GridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Label Label2 = (Label)FindControl("Label2");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                EluZSProvider.Project rowview = (EluZSProvider.Project)e.Row.DataItem;
                string temp = rowview.补偿实施审批状态.ToString(); 
                if (temp == "1") { }
                else
                {
                    e.Row.Cells[7].Text = "编辑";
                    //e.Row.Cells[8].Text = "查看";
                    e.Row.Cells[7].Enabled = false;
                    //e.Row.Cells[8].Enabled = false;
                    Label2.Text = "请在征收补偿实施方案申请中提交，审核通过后才能进行征收决定申请的维护！";
                }
            }
        }
        protected string GetSqlWhere() {
            string sqlWhere = string.Empty;
            TextBox xmmc = (TextBox)FindControl("xmmc");
            TextBox lxh = (TextBox)FindControl("lxh");
            TextBox xkzh = (TextBox)FindControl("xkzh");
            TextBox jsdw = (TextBox)FindControl("jsdw");
            if (!string.IsNullOrEmpty(xmmc.Text))
            {
                sqlWhere += " and 项目名称  like '%" + xmmc.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(lxh.Text))
            {
                sqlWhere += " and 立项号 like '%" + lxh.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(xkzh.Text))
            {
                sqlWhere += " and id in (select pid from zs_prono where 征收许可证号 like '%" + xkzh.Text.Trim() + "%')";
            }
            if (!string.IsNullOrEmpty(jsdw.Text))
            {
                sqlWhere += " and 建设单位 like '%" + jsdw.Text.Trim() + "%'";
            }
//            if (_EluZSDataProvider.IsQZSBM(userid))
//            {
//                sqlWhere += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
//Where  t.userid Like '%" + userid + "%') Like '%'||区属||'%' ";
//            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sqlWhere += @" and (Select max(Rolename) Rolename FROM (SELECT sys_connect_by_path(Rolename,',') Rolename FROM (SELECT Rolename,rn rchild,(rn-1) rfather FROM (SELECT   t.Rolename,row_number() over (ORDER BY t.Rolename) rn FROM (Select Tt.Rolename From Zs_User_In_Role t Join Zs_Role Tt On t.Roleid = Tt.Id Where t.Userid Like '%" + userid + "%') t)) CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0')) Like '%' || substr(区属,0,2) || '%'";
            }
            sqlWhere += " order by addtime desc";
            return sqlWhere;
        }
    }
}


